{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 利用 Paddle Quantum 的 qchem 模块进行量子化学计算\n",
    "_Copyright (c) 2021 Institute for Quantum Computing, Baidu Inc. All Rights Reserved._\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "qchem 是基于 Paddle Quantum 推出的用于量子化学研究的工具集。qchem 为量子化学领域的研究者提供了一系列工具，使他们可以利用量子计算方法完成量子化学任务。与此同时，qchem 也提供了方便开发者进行功能拓展的方式。目前，qchem 正处于开发之中，您可以将需求和建议通过 GitHub 的 issue 或 pull request 反馈给我们，我们会及时给出回复。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 分子基态能量计算\n",
    "qchem 为量子化学计算提供了很多便捷的工具。目前，qchem 模块支持下列分子波函数模版线路：\n",
    "* Hardware Efficient ansatz[<sup>1</sup>](#refer-1),\n",
    "* Hartree Fock ansatz[<sup>2</sup>](#refer-2),\n",
    "* Unitary Coupled Cluster singles and doubles (UCCSD) ansatz[<sup>3</sup>](#refer-3).\n",
    "\n",
    "让我们从具体的例子出发了解 qchem 的使用方法，下面我们演示了利用 qchem 求解氢分子基态的过程。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddle_quantum as pq\n",
    "from paddle_quantum import qchem as pq_qchem\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "import logging\n",
    "logging.basicConfig(level=logging.INFO)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "接下来，我们会利用分子的一些主要性质，包括：分子的几何结构、分子的电荷、计算需要用到的量子化学基函数等，来构建一个 qchem 中的 `Molecule` 类。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# `driver` 用来计算分子中的各种积分\n",
    "driver = pq_qchem.PySCFDriver()\n",
    "\n",
    "# 通过氢分子的性质构造一个 Molecule 类，注：长度单位为埃\n",
    "mol = pq_qchem.Molecule(\n",
    "    geometry=[(\"H\", [0.0, 0.0, 0.0]), (\"H\", [0.0, 0.0, 0.74])],\n",
    "    basis=\"sto-3g\",\n",
    "    multiplicity=1,   \n",
    "    driver=driver\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后，我们需要为氢分子选择一种波函数模版线路。我们选择 `HardwareEfficient` 作为模版线路。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:\n",
      "#######################################\n",
      "Molecule\n",
      "#######################################\n",
      "INFO:root:H2\n",
      "INFO:root:Geometry:\n",
      "INFO:root:H 0.00000, 0.00000, 0.00000\n",
      "H 0.00000, 0.00000, 0.74000\n",
      "INFO:root:Charge: 0\n",
      "INFO:root:Multiplicity: 1\n",
      "INFO:root:Unit: Angstrom\n",
      "INFO:root:\n",
      "#######################################\n",
      "SCF Calculation (Classical)\n",
      "#######################################\n",
      "INFO:root:Basis: sto-3g\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "converged SCF energy = -1.11675930739643\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:SCF energy: -1.11676.\n"
     ]
    }
   ],
   "source": [
    "# 构建 HardwareEfficient 线路.\n",
    "mol.build()\n",
    "n_qubits = mol.num_qubits\n",
    "depth = 2\n",
    "cir = pq_qchem.HardwareEfficient(n_qubits, depth)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "完成上面的步骤之后，我们可以调用 `GroundStateSolver` 求解器，并利用 PaddlePaddle 中的优化器来训练参数化量子线路。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:root:\n",
      "#######################################\n",
      "VQE (Ground State)\n",
      "#######################################\n",
      "INFO:root:Number of qubits: 4\n",
      "INFO:root:Ansatz: HardwareEfficient\n",
      "INFO:root:Optimizer: Adam\n",
      "INFO:root:\tlearning_rate: 0.5\n",
      "INFO:root:\n",
      "Optimization:\n",
      "INFO:root:\tItr 0, loss=-0.32028.\n",
      "INFO:root:\tItr 10, loss=-1.02221.\n",
      "INFO:root:\tItr 20, loss=-1.12635.\n",
      "INFO:root:\tItr 30, loss=-1.13166.\n",
      "INFO:root:\tItr 40, loss=-1.13439.\n",
      "INFO:root:\tItr 50, loss=-1.13665.\n",
      "INFO:root:\tItr 60, loss=-1.13680.\n",
      "INFO:root:Optimization converged after 65 iterations.\n",
      "INFO:root:The final loss = -1.13705.\n"
     ]
    }
   ],
   "source": [
    "# 选择 paddlepaddle 中的 Adam 优化器\n",
    "from paddle.optimizer import Adam\n",
    "\n",
    "solver = pq_qchem.GroundStateSolver(Adam, num_iterations=100, tol=1e-5, save_every=10)\n",
    "e, psi = solver.solve(mol, cir, learning_rate=0.5)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "## 参考文献\n",
    "\n",
    "[1] Kandala, Abhinav, et al. \"Hardware-efficient variational quantum eigensolver for small molecules and quantum magnets.\" [Nature 549.7671 (2017): 242-246.](https://www.nature.com/articles/nature23879)\n",
    "\n",
    "[2] Arute, Frank, et al. \"Hartree-Fock on a superconducting qubit quantum computer.\" [Science 369.6507 (2020): 1084-1089.](https://www.science.org/doi/10.1126/science.abb9811)\n",
    "\n",
    "[3] Abhinav, Aspuru-Guzik, et al. \"A Quantum Computing View on Unitary Coupled Cluster Theory\" (https://arxiv.org/abs/2109.15176)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "modellib",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.15"
  },
  "vscode": {
   "interpreter": {
    "hash": "8f24120f890011f53feb4ed62c47961d8565ec1de8b7cb23548c15bd6da8f2d2"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
